package spade;


class JID {
    String node;
    String domain;
    String resource;

    //""" JID object. JID can be built from string, modified, compared, serialised into string. """
    public JID() {
        node = "";
        domain = "";
        resource = "";
    };

    public JID(String j) {
        int index = -1;

        index = j.indexOf('@');

        if (index == -1) return;
        node = j.substring(0, index);
        domain = j.substring(index+1, j.length());

        index = domain.indexOf('/');
        if (index == -1) return;
        resource = domain.substring(index+1, domain.length());
        domain = domain.substring(0, index);

        return;
    };

    public JID(String n, String d) {
        node = n;
        domain = d;
    };

    public JID(String n, String d, String res) {
        node = n;
        domain = d;
        resource = res;
    };

    public String getNode() {
        //""" Return the node part of the JID """
        return node;
    };

    public void setNode(String n) {
        //""" Set the node part of the JID to new value. Specify None to remove the node part."""
        node = n.toLowerCase();
    };

    public String getDomain() {
        //""" Return the domain part of the JID """
        return domain;
    };

    public void setDomain(String d) {
        //""" Set the domain part of the JID to new value."""
        domain = domain.toLowerCase();
    };

    public String getResource() {
        //""" Return the resource part of the JID """
        return resource;
    };

    public void setResource(String res) {
        //""" Set the resource part of the JID to new value. Specify None to remove the resource part."""
        resource = res;
    };

    public String getStripped() {
        //""" Return the bare representation of JID. I.e. string value w/o resource. """
        return node + "@" + domain;
    };

    public boolean __eq__(JID other) {
        if (node != other.node) return false;
        if (domain != other.domain) return false;
        if (resource != other.resource) return false;
        return true;
    };

    public boolean __eq__(String j) {
        //""" Compare the JID to another instance or to string for equality. """
        JID other = new JID(j);
        return this.__eq__(other);
    };

    public boolean __ne__(JID other) {
        //""" Compare the JID to another instance or to string for non-equality. """
        return !(this.__eq__(other));
    };

    public boolean __ne__(String j) {
        JID other = new JID(j);
        return !(this.__eq__(other));
    };

    public boolean bareMatch(JID other) {
        //""" Compare the node and domain parts of the JID's for equality. """
        if (node != other.node) return false;
        if (domain != other.domain) return false;
        return true;
    };

    public String asString(boolean wresource) {
        //""" Serialise JID into string. """
        if (wresource) return node+"@"+domain+"/"+resource;
        else return node+"@"+domain;
    };

    public String asString() {
        return this.asString(true);
    };

    public int hashCode() {
        //""" Produce hash of the JID, Allows to use JID objects as keys of the dictionary. """
        return this.asString().hashCode();
    };
};
